﻿using System;
using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Raspare;

namespace droid
{
    [Activity(Label = "Välj karaktär")]
    public class AbilityActivity : ListActivity
    {
        List<string> items;

        Karaktär character;
        private List<Färdighet> färdigheter = new List<Färdighet>();

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            character = ((OldShowActivity)Parent).character;

            if (character != null)
            {
                //Load screen
                ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, GetAbilities());
            }
            
        }

        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            var input = färdigheter[position];

            int value = input.Värde;
            Random rnd = new Random();
            int firstRoll = rnd.Next(1, 11);
            int secondRoll = 0;
            if (firstRoll == 10)
            {
                firstRoll = 10;
                secondRoll = rnd.Next(1, 11);
            }
            if (firstRoll == 1)
            {
                firstRoll = 0;
                secondRoll = -rnd.Next(1, 11);
            }
            int diceResult = value + firstRoll + secondRoll;
            string diceRoll = "[" + (firstRoll == 0 ? 1 : firstRoll) + (secondRoll != 0 ? "," + secondRoll : "") + "]";
            Toast.MakeText(this, input.Namn + "(" + input.Värde + "): " + diceRoll + " " + diceResult, ToastLength.Short).Show();

            ((OldShowActivity)Parent).AddToHistory(input.Namn + "(" + input.Värde + "): " + diceRoll + " " + diceResult);
        }

        private List<string> GetAbilities()
        {
            items = new List<string>();
            färdigheter = new List<Färdighet>();
            foreach (var item in character.Färdigheter)
            {
                if (item.Värde > 0)
                {
                    färdigheter.Add(item);
                    string abilityString = item.Namn + " ";
                    if (item.Specialiseringar.Length > 0)
                    {
                        abilityString += "(" + String.Join(", ", item.Specialiseringar) + ") ";
                    }
                    abilityString += item.Värde;
                    items.Add(abilityString);
                }
            }

            return items;
        }
    }
}

